#include <iostream>
#include <exception>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int getFirstK(int* data, int length, int k, int start, int end)
{
    if(start>end)
    {
        return -1;
    }
    int middleIndex = (start+end)/2;
    int middleData = data[middleIndex];

    if(middleData==k)
    {
        if((middleIndex>0 && data[middleIndex-1]!=k) || middleIndex==0 )
            return middleIndex;
        else
            end = middleIndex-1;
    }
    else if(middleData>k)
        end = middleIndex-1;
    else
        start = middleIndex+1;

    return getFirstK(data, length, k, start, end);
}
int main()
{
   int arr[] = {1,2,3,3,3,3,4,5};
   int n = getFirstK(arr, 1, 4, 0, 7);

    cout<<n<<endl;
    return 0;
}
